package com.o2o.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.springframework.stereotype.Controller;

import com.o2o.base.BaseControllerAction;
import com.o2o.content.FileContent;
import com.o2o.util.FileUtil;
import com.o2o.util.MD5Util;

@Controller
public class CommonControllerAction extends BaseControllerAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3182686094980333373L;
	
	private String f = ""; //以; 截取的资源文件
	
	private InputStream inputStream;
	
	public 	String combo(){
		String path = request.getContextPath();
		try {
			String[] file = f.split(";");
			inputStream = new FileInputStream("c:\\temp\\file.txt");
		} catch (FileNotFoundException e) {
			logger.error("请求资源文件未找到 f:"+f);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	private File productPicture;
	private String productPictureFileName;
	private String productPictureFileContentType;
	/**
	 *上传图片文件 
	 */
	public String picFileUpload(){
		long uid= 1;
		String path = request.getSession().getServletContext().getRealPath("");
		System.out.println("path:"+path);
		String extensionName = FileUtil.getExtensionName(productPictureFileName);
		try {
			String fileName = MD5Util.getFileMD5String(productPicture)+"."+extensionName;//文件名是文件的内容摘要md5
			String folder = "/upload"+"/"+uid+"/picture/";
			String projectPath = folder+fileName;
			File folderFile = new File(path+folder);
			if  (!folderFile.exists()  && !folderFile.isDirectory()){       //没有文件夹则创建
				folderFile.mkdirs();    
			}
			String fileType = FileUtil.getFileContentType(productPicture);
			if(!(fileType.equals(FileContent.JPEG) || fileType.equals(FileContent.GIF) || fileType.equals(FileContent.PNG))){
				result.put("message", "请上传图片类型文件");
				result.put("upload_success", false);
				result.put("timestamp", System.currentTimeMillis());
			}else{
				File tmpFile = new File(path+folder+fileName);
				boolean saveFile = false;
				if(!tmpFile.exists()){//判断文件是否已经上传过
					saveFile = FileUtil.saveFile(productPicture, path+folder+fileName);
				}else{
					saveFile = true;
				}
				if(saveFile){
					result.put("message", "上传图片文件成功");
					result.put("success", true);
					result.put("picture_path", projectPath);
					result.put("timestamp", System.currentTimeMillis());
				}else{
					result.put("message", "上传图片文件失败");
					result.put("success", false);
					result.put("timestamp", System.currentTimeMillis());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	

	



	public void setProductPicture(File productPicture) {
		this.productPicture = productPicture;
	}







	public void setProductPictureFileName(String productPictureFileName) {
		this.productPictureFileName = productPictureFileName;
	}







	public void setProductPictureFileContentType(
			String productPictureFileContentType) {
		this.productPictureFileContentType = productPictureFileContentType;
	}







	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

}
